package socket

import (
	"common/mildware"
	"common/models"
	"common/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
	"project/docModel"
	"strconv"
	"strings"
)

// SearchFriendPeople 查找朋友
func (h SHandler) SearchFriendPeople(c *gin.Context) {
	resp := new(models.RespData)
	var users []*models.UserInfo
	//获得用户的token
	id, _, _, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("socket : mildware.GetCyrrentUserID err ", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	exist, info, err := h.M.UserCheckId(id)
	if err != nil {
		zap.L().Error("socket :h.M.UserCheckId err ", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if exist {
		firends := strings.Split(info.Friends, "|")
		for _, ID := range firends {
			if ID == "" {
				continue
			}
			fExist, friend, err := h.M.UserCheckId(ID)
			if err != nil {
				zap.L().Error("socket :h.M.UserCheckId err ", zap.Error(err))
				resp.SendServerBusy(c)
				return
			}
			if fExist {
				users = append(users, friend)
			}
		}
		resp.Data = users
		resp.SendSuccess(c)
	}
}

// AddFriend 添加朋友
func (h SHandler) AddFriend(c *gin.Context) {
	var (
		friends []string
	)
	resp := new(models.RespData)
	req := new(docModel.AddFriendReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Socket:AddFriend with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Socket:AddFriend with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//获得用户的信息
	ID, _, _, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("socket :mildware.GetCyrrentUserID err ", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	exist, userInfo, err := h.M.UserCheckId(ID)
	if !exist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	//获得朋友的信息
	exist, friendsInfo, err := h.M.UserCheckUserID(req.FriendID)
	if err != nil {
		zap.L().Error("socket :mildware.GetCyrrentUserID err ", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	if !exist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	friends = strings.Split(userInfo.Friends, "|")
	fExist, _ := models.CheckList(friends, req.FriendID)
	if fExist {
		resp.SendCode(c, models.CodeFriendExist)
		return
	}
	//判断是否是第一次添加
	if userInfo.Friends == "|" {
		userInfo.Friends = strconv.Itoa(int(friendsInfo.ID))
	} else {
		userInfo.Friends = models.AddListToString(friends, strconv.Itoa(int(friendsInfo.ID)))
	}
	resp.SendSuccess(c)
}
func (h SHandler) DelFriend(c *gin.Context) {
	resp := new(models.RespData)
	req := new(docModel.DelFriendReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Socket:DelFriend with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Socket:DelFriend with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//获得id
	ID, _, _, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		resp.SendServerBusy(c)
		return
	}
	exist, info, err := h.M.UserCheckId(ID)
	if !exist {
		resp.SendCode(c, models.CodeUserNotExist)
		return
	}
	friends := strings.Split(info.Friends, "|")
	info.Friends = models.RemoveList(req.FriendId, friends)
	//更新
	if err := h.M.UserUpdateById(info); err != nil {
		zap.L().Error("Socket:DelFriend h.M.UserUpdateById", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}
